// sub_renyuanguanli/renyuanlist/renyuanlist.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: "",
    parentBankname: "",
    bankindex: "",
    bankInfo: {},
    shortname: "",
    searchValue: "",
    isfirstAdmin: false,
    isManage: false,
    list: [],
    pageNum: 1,
    pageSize: 20,
    selectCheckbox: [],
    total: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.initInfos()
  },
  async initInfos() {
    await this.queerBankInfo()
    this.getList()
  },
  onSearch(e) {
    console.log(e);
    this.setData({
      searchValue: e.detail,
    })
    this.readygetList()
  },

  selectChange(e) {
    this.setData({
      selectCheckbox: e.detail,
    });
  },
  cellCheckToggle(e) {
    const {
      index
    } = e.currentTarget.dataset;
    const checkbox = this.selectComponent(`.checkboxes-${index}`);
    checkbox.toggle();
  },
  // method
  getList() {
    const params = {
      corpName: this.data.searchValue,
      userId: "",
      bankId: this.data.bankInfo.bankId,
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize
    }
    console.log(params, 'params');
    getApp().globalData.request(
      "/corpVisit/getCorpPage", params,
      "POST"
    ).then(res => {
      console.log(res, '列表数据');
      this.setData({
        list: this.data.list.concat(res.data.list),
        total: res.data.total
      })
    })
  },
  // 判断是否是一级管理员
  isFiradmin() {
    let arr = wx.getStorageSync("userInfo").corpRole;
    // let result = arr.find((item,index)=>{
    //   return item == "fir_admin"
    // })
    if (arr.indexOf("fir_admin") !== -1) {
      this.setData({
        isfirstAdmin: true,
      })
    } else {
      this.setData({
        isfirstAdmin: false,
      })
    }
  },
  // 查询银行信息
  async queerBankInfo() {
    await getApp().globalData.request(
      "/corpVisit/query_user_bank_info", {},
      "POST"
    ).then(res => {
      console.log(res);
      if (res.code == 200) {
        this.setData({
          bankInfo: res.data
        })
      }
    })
  },

  goOne(e) {
    const oneObj = this.data.list.find(val => val.corpCredit == e.currentTarget.dataset.id)
    if (oneObj.checked) {
      oneObj.checked = false
    } else {
      oneObj.checked = true
    }
    // 是否全选了
    if (!this.data.list.some(val => !val.checked)) {
      this.setData({
        ischecked: true
      })
    } else {
      this.setData({
        ischecked: false
      })
    }
    this.setData({
      list: this.data.list
    })
  },
  ischeckedChange(e) {
    // console.log(e);
    if (e.detail) {
      // 全选
      this.data.list.forEach((item, index) => {
        item.checked = true
        this.data.selectCheckbox.push(item.corpCredit)
      })
      this.setData({
        selectCheckbox: this.data.selectCheckbox,
        list: this.data.list
      })
    } else {
      this.data.list.forEach((item, index) => {
        item.checked = false
      })
      this.setData({
        list: this.data.list,
        selectCheckbox: []
      })
    }
    this.setData({
      ischecked: e.detail,
    });
  },
  // 去详情 
  toDetailClick(e) {
    wx.navigateTo({
      url: `/sub_renyuanguanli/person_detail/person_detail?id=${e.currentTarget.dataset.uid}&shortname=${this.data.shortname}`,
    })
  },
  fenpeiClick(e) {
    const list = this.data.list.filter(val => val.checked)
    if (list.length == 0) {
      wx.showToast({
        title: '请选择企业',
        icon: 'none'
      })
      return
    }
    // 找出勾选的项
    wx.setStorageSync('claimLit', list)
    wx.navigateTo({
      url: `/sub_company_claim/page2/page2?bankId=${this.data.bankInfo.bankId}`,
    })
  },
  readygetList() {
    this.setData({
      pageNum: 1,
      list: []
    })
    this.getList()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  scrollBottom() {
    if (this.data.list.length < this.data.total) {

      this.setData({
        pageNum: this.data.pageNum * 1 + 1,
      })
      // console.log(this.data.pageNum);
      this.getList()
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (this.data.refresh) {
      this.readygetList()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})